package GUI;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class LowerPanel {
	HorizontalScrollBar hsb;
	PlayPauseButton ppb;
	int x,y;
	float delay;
	GregorianCalendar dateEndBackup;
	GregorianCalendar dateStartBackup;
	int dateEndIndexBackup;
	int dateStartIndexBackup;


	
	public LowerPanel(ArrayList<GregorianCalendar> d,int x,int y) {
		super();
		this.x=x;
		this.y=y;
		this.ppb = new PlayPauseButton(x,y-4,25,25);

		hsb = new HorizontalScrollBar(d,false);
		hsb.x=x+30;
		hsb.y=y;
		hsb.width=966;
		hsb.height=15;
		hsb.size = (float)2/ d.size();
		
	}
	
	public void draw(float dt){
		
		drawBackGroud();
		
		if(ppb.playT){
			hsb.drawBackLayer();
			drawMovieLine(dt);
		}
		else {delay=0;
		dateStartBackup = GV.dateStart;
		dateEndBackup = GV.dateEnd;
		dateEndIndexBackup = GV.dateEndIndex;
		dateStartIndexBackup = GV.dateStartIndex;
		hsb.draw();
		}
		ppb.draw();
		
	}
	private void drawBackGroud() {
		GV.processing.strokeWeight(2);
		GV.processing.rectMode(GV.processing.CORNER);
		GV.processing.fill(GV.backGroundColor);
		GV.processing.stroke(80);
		//GV.processing.rect(0,y-10,GV.width,GV.height-y+10);
		GV.processing.rect(0,y-10,GV.width,GV.height-y+10);

	}
	
	public void drawMovieLine(float dt){
		
		delay+=dt;
		int val = (GV.dateEnd.get(Calendar.YEAR)-2001)*12+GV.dateEnd.get(Calendar.MONTH)+1;
		if(delay >1){
			GregorianCalendar d = GV.getDateFromMonthNumber(val);
			if(d.before(dateEndBackup)){
			//GV.dateStart = GV.getDateFromMonthNumber(val);
			//GV.dateStartIndex=val;
			GV.dateEnd = GV.getDateFromMonthNumber(val);
			GV.dateEndIndex=val;
			//hsb.valEnd = GV.processing.map(val+2, 0, hsb.total-1,(float)-0.02, (float)0.98);
			//hsb.valStart = GV.processing.map(val, 0, hsb.total-1,0, 1);
			delay=0;
			}
			else {ppb.playT=false;
				restoreStartEndIndices();
			}
			
		}
		
		
		float valEnd =  GV.processing.map(val,  1, GV.totalMonths,0, hsb.width);
		int val2 = (GV.dateStart.get(Calendar.YEAR)-2001)*12+GV.dateStart.get(Calendar.MONTH)+1;
		float valStart =  GV.processing.map(val2,  1, GV.totalMonths,0, hsb.width);

		GV.processing.strokeWeight(2);
		GV.processing.stroke(GV.processing.color(251,87,5));
		GV.processing.line(valEnd+hsb.x, y-10, valEnd+hsb.x, y+20);
		GV.processing.line(valStart+hsb.x, y-10, valStart+hsb.x, y+20);
		GV.processing.line(valStart+hsb.x, y+(float)7.5, valEnd+hsb.x, y+(float)7.5);

		
		GV.processing.rectMode(GV.processing.CORNER);
		GV.processing.fill(GV.backGroundColor);
		GV.processing.stroke(80);
		GV.processing.rect(valEnd+hsb.x-45,y-40,90,20);
		GV.processing.rect(valStart+hsb.x-45,y+20,90,20);
		GV.processing.fill(GV.textColor);
		GV.processing.textAlign(GV.processing.CENTER);
		GV.processing.text(GV.monthName[GV.dateEnd.get(Calendar.MONTH)]+" "+GV.dateEnd.get(Calendar.YEAR),valEnd+hsb.x, y - 25);
		GV.processing.text(GV.monthName[GV.dateStart.get(Calendar.MONTH)]+" "+GV.dateStart.get(Calendar.YEAR),valStart+hsb.x, y + 35);
		
	}
	
	public void restoreStartEndIndices(){
		GV.dateEnd = dateEndBackup;
		GV.dateEndIndex = dateEndIndexBackup;
		GV.dateStart = dateStartBackup;
		GV.dateStartIndex = dateStartIndexBackup;
		//int valEnd = (GV.dateEnd.get(Calendar.YEAR)-2001)*12+GV.dateEnd.get(Calendar.MONTH);
		//int valStart = (GV.dateStart.get(Calendar.YEAR)-2001)*12+GV.dateStart.get(Calendar.MONTH);
		//hsb.valEnd = GV.processing.map(valEnd, 0, hsb.total-1,(float)-0.02, (float)0.98);
		//hsb.valStart = GV.processing.map(valStart, 0, hsb.total-1,0, 1);
	}

	public boolean mouseOverArea() {
		if(GV.processing.mouseX > 0 && GV.processing.mouseX <  GV.width && 
				GV.processing.mouseY > y-10 && GV.processing.mouseY < GV.height ) 	return true;
		return false;
	}
	
	
}
